<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="品牌的名称" prop="brandName">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入品牌的名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="品牌联系人电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入品牌联系人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:brand:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:brand:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:brand:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:brand:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="brandList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="品牌id" align="center" prop="id" />
      <el-table-column label="运营方式" align="center" prop="selfOperated" >
        <template slot-scope="scope">
           {{selfOperatedMap[scope.row.selfOperated]}}
        </template>
        </el-table-column>
      <el-table-column label="品牌的名称" align="center" prop="brandName" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="品牌的icon" align="center" prop="icon" >


         <template slot-scope="scope" v-if="scope.row.icon">
                    <div class="image-box">
                        <el-image
                          style="width: 50px; height: 50px; margin-right: 5px"
                          :src="IMAGE_OSS_URL+'/'+scope.row.icon"
                          :preview-src-list="[...IMAGE_OSS_URL+scope.row.icon]"
                        >
                    </el-image>
                    </div>
                  </template>


        </el-table-column>
      <el-table-column label="品牌联系人电话" align="center" prop="phone" />
      <!-- <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
           {{scope.row.status == 1 ? '上架':'下架'}}
        </template>
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:brand:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:brand:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改店铺的品牌对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="来源平台" prop="platformType">

             <el-select v-model="form.platformType">
                        <el-option
                      v-for="item in channelList"
                      :key="item.code"
                      :label="item.label"
                      :value="item.code">
                </el-option>
              </el-select>

        </el-form-item>

        <el-form-item label="品牌的名称" prop="brandName">
          <el-input v-model="form.brandName" placeholder="请输入品牌的名称" />
        </el-form-item>

         <el-form-item label="选择运营方式" prop="selfOperated">
          <el-select v-model="form.selfOperated" clearable placeholder="请选择">
              <el-option
                v-for="item in selfOperatedList"
                :key="item.code"
                :label="item.label"
                :value="item.code">
              </el-option>
            </el-select>
        </el-form-item>

         <el-form-item label="品牌联系人电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入品牌联系人电话" />
        </el-form-item>


        <el-form-item label="来客id" prop="accountid"  v-if="form.platformType=='DY'">
          <el-input v-model="form.accountid" placeholder="请输入来客id" />
        </el-form-item>

           <el-form-item label="appid" prop="appid"  v-if="form.platformType=='DY'">
          <el-input v-model="form.appid" placeholder="输入抖音小程序得id" />
        </el-form-item>

        <el-form-item label="appsecret" prop="appsecret" v-if="form.platformType=='DY'">
          <el-input v-model="form.appsecret" placeholder="输入抖音小程序得密钥" />
        </el-form-item>

           <el-form-item label="品牌网址" prop="website">
          <el-input v-model="form.website" placeholder="输入品牌网址" />
        </el-form-item>


        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address"  placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="品牌公司名称" prop="company">
          <el-input v-model="form.company" placeholder="输入品牌公司名称" />
        </el-form-item>






        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>


          <el-form-item
                  label="详情图片"
                  prop="icon"
                  :rules="[
                    { required: true, message: '请上传详情图片', trigger: 'change' },
                  ]"
      >
        <el-upload
          :action="`${BASE_URL}/common/images/upload`"
          accept="image/jpeg,image/jpg,image/png"
          list-type="picture-card"
          :limit="1"
          :file-list="detailPictureList"
          :on-remove="handleDetailUploadChange"
          :on-change="handleDetailUploadChange"
          :before-upload="beforeAvatarUpload"
        >
          <i class="el-icon-plus" ></i>
        </el-upload>
      </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBrand, getBrand, delBrand, addBrand } from "@/api/brand/index";
import { getChannel } from "@/api/common/index";
import { BASE_URL, IMAGE_OSS_URL , selfOperatedList , selfOperatedMap } from "@/config/index";
export default {
  name: "Brand",
  data() {
    return {
      // 遮罩层
      loading: true,
      selfOperatedMap,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      selfOperatedList,
      detailPictureList: [],
      channelList:[],
      // 总条数
      total: 0,
      // 店铺的品牌表格数据
      brandList: [],
      BASE_URL,
      IMAGE_OSS_URL,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brandName: null,
        address: null,
        icon: null,
        phone: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      platformType:[

            { required: true, message: "请选择平台", trigger: "blur" }

      ],

     selfOperated:[

            { required: true, message: "请选择运营方式", trigger: "blur" }

      ],
      
      accountid:[

            { required: true, message: "来客id账号", trigger: "blur" }

      ],

       appid:[

            { required: true, message: "appid不能为空", trigger: "blur" }

      ],

       appsecret:[

            { required: true, message: "appsecret不能为空", trigger: "blur" }

      ],

        company:[

            { required: true, message: "公司名称不能为空", trigger: "blur" }

      ],

        website:[
            { required: true, message: "品牌网址不能为空", trigger: "blur" }
      ],

        address:[
            { required: true, message: "详细地址不能为空", trigger: "blur" }
      ],

        phone: [
          { required: true, message: "请输入店铺电话", trigger: "blur" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],




        brandName: [
          { required: true, message: "品牌的名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态0删除  1上架  2下架不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    getChannel().then(res=>{
        let arr=res.data
        this.channelList.push(arr[0])
        this.channelList.push(arr[1])
      })
  },
  methods: {



    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 10;

      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 10MB!");
      }
      return isLt2M;
    },

    handleDetailUploadChange(file, fileList) {
      this.detailPictureList = fileList;
      this.form.icon = fileList.map((item) => item.url).join(",");
    },


    /** 查询店铺的品牌列表 */
    getList() {
      this.loading = true;
      let param=this.queryParams;
      // param.status=1
      listBrand(param).then(response => {
        this.brandList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        brandName: null,
        address: null,
        icon: null,
        phone: null,
        status: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺的品牌";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBrand(id).then(response => {
        this.form = response.data;
          this.detailPictureList = this.form.icon.split(",").map((item) => ({
          url: IMAGE_OSS_URL + item,
          status: "success",
          response: {
            data: item,
          },
        }));
        this.open = true;
        this.title = "修改店铺的品牌";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.platformType!='DY'){
       this.rules.appid=[]
       this.rules.appsecret=[]
       this.rules.accountid=[]
      }

      this.$refs["form"].validate(valid => {
        if (valid) {
          let form={... this.form}
          form.icon=this.detailPictureList
              .map((item) => item.response.data)
              .join(",");
          if (form.id != null) {
            addBrand(form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBrand(form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除店铺的品牌编号为"' + ids + '"的数据项？').then(function() {
        return delBrand(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/brand/export', {
        ...this.queryParams
      }, `brand_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
